<?php
/**退租model*/
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Outrent extends Model
{
    /**
     * 【使用过优惠金额暂时当现金】
     * 退租操作【优先退钱后退分，优先退租金后退信用额度【当前处理先退积分，逻辑此处待修改=》主要修改混合支付的地方,退租转租额度】】
     * 【该用户其他订单的信用额度使用不在这里关联，会引起查询误区，修改逻辑教繁琐】
     * 【退租处理的时候：在租订单修改；额度修改；日志记录，金额；退租表记录；账单表修改状态，数值等【结算当前该详情账单】】
     * 【转租处理的时候：生成新的订单号和订单详情；账单【结清本账单,下月账单按随租处理，起租日期，按原下月算，本月已结清】；修改原订单详情账单数据；【说明:在原订单增加订单详情的话，不方便用户在我的中心查看，转租视作新租】】
     * 【退单时对订单及详情操作，尽量只修改少部分，做增减操作，方便账单，用户订单查看】
     */
    public static function insert_outrent($data)
    {
        /**
         * 账单操作【转租要新生成，退租不生成，放置单独模，退租，转租原订单详情数量均在此处修改】
         * 退租表操作
         * 1【记录退租表操作:退租数量,退租订单详情单号，退租状态，退租时间】2【对退租的在租详情表中在租商品数量修改】
         */
        $insert_outrent = DB::table('outrent')->insertGetId([
            'userid' => $data['userid'],
            'details_orderNo' => $data['details_orderNo'],
            'order_details_id' => $data['order_details_id'],
            'fitment_details_id' => $data['fitment_details_id'],
            'fitment_details_number' => $data['fitment_details_number'],
            'outrent_type' => $data['outrent_type'],//1退租2转租
            'outrent_rent_orderid' => $data['outrent_rent_orderid'],
            'details_rent_type' => $data['details_rent_type'],
            'outrent_bill' => $data['outrent_bill'],
            'outrent_bond' => $data['outrent_bond'],
            'outrent_amount' => $data['outrent_amount'],
            'outrent_reason' => null,
            'checktime' => null,
            'isdel' => 1,
            'status' => 1,
            'created_at' => $data['created_at'],
            'updated_at' => $data['updated_at']
        ]);
        DB::beginTransaction();
        try{
            if(!$insert_outrent) {
                DB::rollback();return '记录退租数据失败!';
            }
            //原始订单详情表
            $old_orderdt = DB::table('order_details')->where('id',$data['order_details_id'])->first();
            //订单信息
            $common_order = DB::table('orders')->where('id',$data['order_id'])->first();
            //退租用户信息
            $common_user = DB::table('frontusers')->where('id',$data['userid'])->first();
            //退租用户额度表
            $common_quota = DB::table('quota')->where('quota_user_id',$data['userid'])->first();
            //本地在租操作关联的额度值
            $common_sum_quota = $data['rentback_bond']*$data['fitment_details_number'];
            //在租订单详情账单
            $common_bill_data = DB::table('bill')->where('orderdtid',$data['order_details_id'])->orderBy('updated_at','desc')->first();
            //在租详情表更新
            $out_rent_orderdt_edit = DB::table('order_details')->where('id',$data['order_details_id'])
                ->update(['rent_num'=>$old_orderdt->rent_num-$data['fitment_details_number'], 'updated_at'=>date('Y-m-d H:i:s',time())]);
            if(!$out_rent_orderdt_edit) {
                DB::rollback();return '在租详情表更新修改失败!';
            }
            //【修改后在租详情表详情】
            $out_rent_orderdtdt = DB::table('order_details')->where('id',$data['order_details_id'])->first();
            //【统计该订单当前时间在租商品数量】
            $order_rent_orderdt_rent_sum = DB::table('orders as od')->leftJoin('order_details as od_dt','od_dt.details_order_id','=','od.id')->where('od_dt.details_status',1)->where('od_dt.details_orders_isdel',1)->where('od.id',$data['order_id'])
                ->sum('od_dt.rent_num');
            //【统计该订单当前时间售后商品数量】
            $order_rent_orderdt_repair_num = DB::table('orders as od')->leftJoin('order_details as od_dt','od_dt.details_order_id','=','od.id')->where('od_dt.details_status',1)->where('od_dt.details_orders_isdel',1)->where('od.id',$data['order_id'])
                ->sum('od_dt.repair_num');
            /**用户执行退租转租账单当前月及之前账单状态修改*/
            $isexsit_outrent_bill_to_edit_status = DB::table('bill')->where('orderdtid',$data['order_details_id'])->where('isdel',1)->where('status',0)->where('months_bill','<',date('Y-m-d',time()))->orwhere('months_bill','like','%'.date('Y-m').'%')->first();
            if($isexsit_outrent_bill_to_edit_status){ //校验是否有需要修改状态的账单
                $outrent_do_bill_edit = DB::table('bill')->where('orderdtid',$data['order_details_id'])->where('status',0)->where('months_bill','<',date('Y-m-d',time()))->orwhere('months_bill','like','%'.date('Y-m').'%')
                    ->update(['pay_bill_time'=>date('Y-m-d H:i:s',time()),'status'=>1,'updated_at'=>date('Y-m-d H:i:s',time())]);
                if(!$outrent_do_bill_edit) {
                    DB::rollback();return '修改已支付账单状态失败!';
                }
            }
            /**退租涉及表【订单详情表修改,额度表，用户表余额，用户余额详情表【确认退租后修改商品数量】】*/
            if($data['outrent_type']==1)
            {
                if($out_rent_orderdtdt->rent_num+$out_rent_orderdtdt->repair_num<1){ //详情表全部退租
                    /**原订单表操作*/
                    $out_rent_orderdt_edit = DB::table('order_details')->where('id',$data['order_details_id'])
                        ->update(['outrent_num'=>$old_orderdt->outrent_num+$data['fitment_details_number'], 'details_status'=>2]);
                    if(!$out_rent_orderdt_edit){
                        DB::rollback();return '订单详情表更新失败!';
                    }
                    if($order_rent_orderdt_rent_sum+$order_rent_orderdt_repair_num != $common_order->orders_number){ //订单没有在租在租在售后商品
                        $outrent_rent_order_edit_status = DB::table('orders')->where('id',$data['order_id'])
                            ->update(['orders_status'=>6,'updated_at'=>date('Y-m-d H:i:s',time())]);
                        if(!$outrent_rent_order_edit_status) {
                            DB::rollback();return '订单表状态更新失败!';
                        }
                    }
                    /**账单表操作*/
                    $outrent_do_bill_delete = DB::table('bill')->where('orderdtid',$data['order_details_id'])->where('months_bill','>',date('Y-m-d',time()))->delete();
                    if(!$outrent_do_bill_delete) {
                        DB::rollback();return '删除已退租下月账单失败!';
                    }
                }else{ //详情表部分退租
                    /**原订单表操作*/
                    $out_rent_orderdt_edit = DB::table('order_details')->where('id',$data['order_details_id'])
                        ->update(['outrent_num'=>$old_orderdt->outrent_num+$data['fitment_details_number']]);
                    if(!$out_rent_orderdt_edit){
                        DB::rollback();return '订单详情表更新失败!';
                    }
                    /**账单表操作*/
                    $outrent_do_bill_edit_last = DB::table('bill')->where('orderdtid',$data['order_details_id'])->where('months_bill','>',date('Y-m-d',time()))
                        ->update(['num'=>$common_bill_data->num-$data['fitment_details_number'],'amount'=>($common_bill_data->num-$data['fitment_details_number'])*$data['rentback_fix'],'updated_at'=>date('Y-m-d H:i:s',time())]);
                    if(!$outrent_do_bill_edit_last) {
                        DB::rollback();return '修改本月后账单失败!';
                    }
                }
            }//【转租】【订单详情修改，额度，订单，订单详情】
            else {
                if($out_rent_orderdtdt->rent_num+$out_rent_orderdtdt->repair_num<1){ //详情表全部退租
                    /**原订单表操作*/
                    $out_rent_orderdt_edit = DB::table('order_details')->where('id',$data['order_details_id'])
                        ->update(['sublet_num'=>$old_orderdt->sublet_num+$data['fitment_details_number'],'details_status'=>2,'updated_at'=>date('Y-m-d H:i:s',time())]);
                    if(!$out_rent_orderdt_edit){
                        DB::rollback();return '订单详情表更新失败!';
                    }
                    if($order_rent_orderdt_rent_sum+$order_rent_orderdt_repair_num != $common_order->orders_number){ //订单没有在租在租在售后商品
                        $outrent_rent_order_edit_status = DB::table('orders')->where('id',$data['order_id'])
                            ->update(['orders_status'=>6,'updated_at'=>date('Y-m-d H:i:s',time())]);
                        if(!$outrent_rent_order_edit_status) {
                            DB::rollback();return '订单表状态更新失败!';
                        }
                    }
                    /**账单表操作*/
                    $outrent_do_bill_delete = DB::table('bill')->where('orderdtid',$data['order_details_id'])->where('months_bill','>',date('Y-m-d',time()))->delete();
                    if(!$outrent_do_bill_delete) {
                        DB::rollback();return '删除原下月账单失败!';
                    }
                }else{ //详情表部分退租
                    /**原订单表操作*/
                    $out_rent_orderdt_edit = DB::table('order_details')->where('id',$data['order_details_id'])
                        ->update(['sublet_num'=>$old_orderdt->sublet_num+$data['fitment_details_number'],'updated_at'=>date('Y-m-d H:i:s',time())]);
                    if(!$out_rent_orderdt_edit){
                        DB::rollback();return '订单详情表更新失败!';
                    }
                    /**账单表操作*/
                    $outrent_order_bill_edit = DB::table('bill')->where('orderdtid',$data['order_details_id'])->where('months_bill','>',date('Y-m-d',time()))
                        ->update(['amount'=>($common_bill_data->num-$data['fitment_details_number'])*$data['rentback_fix'], 'num'=>$common_bill_data->num-$data['fitment_details_number'], 'updated_at'=>date('Y-m-d H:i:s',time())]);
                    if(!$outrent_order_bill_edit) {
                        DB::rollback();return '转租更新原订单详情下月账单失败!';
                    }
                }
                /**转租生成新订单表*/
                $bond = $data['rentback_bond']*$data['fitment_details_number'];
                $orders_deposite = $data['rentback_nimb']*$data['fitment_details_number']*2;
                $orders_rentbill = $data['rentback_nimb']*$data['fitment_details_number'];
                $orders_amount = $orders_deposite+$orders_rentbill;
                $order_data=[
                    'orders_number' => $data['fitment_details_number'],
                    'userid' => $data['userid'],
                    'fitment_details_id'=>$data['fitment_details_id'],
                    'orders_status' => 1,                                     //订单状态0未付款 1已付款，2未发货，3代签收，4已签收，5完成，6退单
                    'orders_addr_id' => $data['adr_id'],                     //收货地址id
                    'orders_count_bond' => $bond,                             //商品需信用额度
                    'orders_quota' => null,                                   //商品使用信用额度【放置到下个逻辑额度表那边处理】
                    'orders_deposite' => $orders_deposite,                    //订单押金
                    'orders_rentbill' => $orders_rentbill,                    //订单租金
                    'orders_discount' => null,                               //转租订单优惠预留目前值为null
                    'orders_amount' => $orders_amount,                        //支付总额
                    'orders_user_require' => null,                           //给卖家留言
                    'created_at' => date('Y-m-d H:i:s',time()),
                    'updated_at' => date('Y-m-d H:i:s',time())
                ];
                $new_order = Order::outrent_insertorder($order_data);
                if(!isset($new_order['res']) && $new_order['res']!='ok') {
                    DB::rollback();return '转租生成新订单表失败!'.$new_order;
                }
                /**转租生成账单表*/
                $outrent_do_bill_get_orderdtid = DB::table('order_details')->where('details_order_id',$new_order['msg'])->first();
                $outrent_do_bill_add = DB::table('bill')->insertGetId([
                    'userid'=>$data['userid'],
                    'price'=>$data['rentback_nimb'],
                    'amount'=>$data['rentback_nimb']*$data['fitment_details_number'],
                    'num'=>$data['fitment_details_number'],
                    'fitment_dt_img'=>$common_bill_data->fitment_dt_img,
                    'orderdtid'=>$outrent_do_bill_get_orderdtid->id,
                    'rent_type'=>1,
                    'status'=>0,
                    'months_bill'=>date('Y-m-d',strtotime('+1 months')),
                    'pay_bill_time'=>null,
                    'rent_start_date'=>date('Y-m-d',strtotime('+1 months')),
                    'rent_end_date'=>date('Y-m-d',strtotime('+12 months')),
                    'isdel'=>1,
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ]);
                if($outrent_do_bill_add<1) {
                    DB::rollback();return '转租生成下月账单失败!';
                }
            }
            /**额度表操作*/
            if($common_order->orders_quota<1 || empty($common_order->orders_quota)) {  //使用现金支付额度【退还商品额度到账户余额】
                $outrent_do_quota_to_account = DB::table('account')->insertGetId([
                    'userid'=>$data['userid'],
                    'orderid'=>$common_order->id,
                    'orderdt_id'=>$out_rent_orderdtdt->id,
                    'out_orderid'=>null,
                    'account_flow'=>1,
                    'type'=>1,
                    'amount'=>$common_sum_quota,
                    'describe'=>'退租商品信用额度现金',
                    'isdel'=>1,
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ]);
                if(!$outrent_do_quota_to_account) {
                    DB::rollback();return '在租订单详情退租额度值记录失败!';
                }
                if($data['outrent_type']==1){ //退租
                    $outrent_do_qutao_to_useraccount = DB::table('frontusers')->where('id',$data['userid'])->update(['account'=>$common_user->account+$common_sum_quota,'updated_at'=>date('Y-m-d H:i:s',time())]);
                    if(!$outrent_do_qutao_to_useraccount) {
                        DB::rollback();return '退租返回额度金额到用户余额失败!';
                    }
                }else{
                    $outrent_do_quota_to_account = DB::table('account')->insertGetId([
                        'userid'=>$common_user->id,
                        'orderid'=>$common_order->id,
                        'orderdt_id'=>$data['order_details_id'],
                        'out_orderid'=>$new_order['msg'],
                        'account_flow'=>2,
                        'type'=>1,
                        'amount'=>$common_sum_quota,
                        'describe'=>'在租订单详情转租',
                        'isdel'=>1,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ]);
                    if($outrent_do_quota_to_account<1){
                        DB::rollback();return '转租订单额度值记录失败!';
                    }
                }
            }
            if($common_order->orders_quota==$common_order->orders_count_bond){ //信用额度分支付【退还信用额度积分】
                $outrent_do_quota_to_quotadt = DB::table('quota_detail')->insertGetId([
                    'quota_detail_quota_id'=>$common_quota->quota_id,
                    'quota_detail_order_id'=>$common_order->id,
                    'quota_detail_amount'=>$common_sum_quota,
                    'quota_detail_status'=>3,//1消费3退回
                    'quota_detail_isdel'=>1,
                    'created_at'=>date('Y-m-d H:i:s',time()),
                    'updated_at'=>date('Y-m-d H:i:s',time())
                ]);
                if($outrent_do_quota_to_quotadt<1) {
                    DB::rollback();return '退租返回额度积分记录详情失败!';
                }
                if($data['outrent_type']==1){ //退租
                    $outrent_do_quota_to_quota = DB::table('quota')->where('quota_user_id',$data['userid'])->update(['quota_current'=>$common_quota->quota_current+$common_sum_quota,'quota_used'=>$common_quota->quota_used-$common_sum_quota,'updated_at'=>date('Y-m-d H:i:s',time())]);
                    if(!$outrent_do_quota_to_quota) {
                        DB::rollback();return '退租额度积分返还额度表失败!';
                    }
                }else{
                    $outrent_do_quota_to_quotadt_data=[
                        'quota_detail_quota_id'=>$common_quota->quota_id,
                        'quota_detail_order_id'=>$new_order['msg'],//消费额度订单表主键；
                        'quota_detail_amount'=>$common_sum_quota,
                        'quota_detail_status'=>1,
                        'quota_detail_isdel'=>1,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ];
                    $outrent_do_quota_to_quotadt_out = DB::table('quota_detail')->insertGetId($outrent_do_quota_to_quotadt_data);
                    if($outrent_do_quota_to_quotadt_out<1) {
                        DB::rollback();return '转租额度详情记录数据失败!';
                    }
                    $outrent_do_quota_to_update_new_order = DB::table('orders')->where('id',$new_order['msg'])->update(['orders_quota'=>$common_sum_quota]);
                    if(!$outrent_do_quota_to_update_new_order){
                        DB::rollback();return '转租额度积分使用更新到转租订单表更新失败!';
                    }
                }
            }
            if($common_order->orders_quota>1 && $common_order->orders_quota<$common_order->orders_count_bond){ //混合支付额度【先退钱后退积分】
                $outrent_do_quota_back_useraccount = DB::table('account')->where('orderid',$data['order_id'])->where('account_flow',1)->where('isdel',1)
                    ->where('type',4)
                    ->sum('amount');//统计订单额度使用已返回的值之和
                $outrent_do_pay_backable_useraccount = ($common_order->orders_count_bond-$common_order->orders_quota)-$outrent_do_quota_back_useraccount;
                if($outrent_do_pay_backable_useraccount>0) { //剩余额度现金还有未返回值
                    if($outrent_do_pay_backable_useraccount-$common_sum_quota>0) { //剩余额度值够返回退租的
                        $outrent_do_quota_to_account = DB::table('account')->insertGetId([
                            'userid'=>$data['userid'],
                            'orderid'=>$common_order->id,
                            'orderdt_id'=>$out_rent_orderdtdt->id,
                            'out_orderid'=>null,
                            'account_flow'=>1,
                            'type'=>1,
                            'amount'=>$common_sum_quota,
                            'describe'=>'退租商品信用额度现金',
                            'isdel'=>1,
                            'created_at'=>date('Y-m-d H:i:s',time()),
                            'updated_at'=>date('Y-m-d H:i:s',time())
                        ]);
                        if(!$outrent_do_quota_to_account) {
                            DB::rollback();return '退租额度余额记录失败!';
                        }
                        if($data['outrent_type']==1){
                            $outrent_do_quota_back_useraccount_edit =  DB::table('frontusers')->where('id',$data['userid'])->update(['account'=>$common_user->account+$common_sum_quota,'updated_at'=>date('Y-m-d H:i:s',time())]);
                            if(!$outrent_do_quota_back_useraccount_edit){
                                DB::rollback();return '退租混合额度回退额度到用户余额失败!';
                            }
                        }else{
                            $outrent_do_quota_to_account = DB::table('account')->insertGetId([
                                'userid'=>$common_user->id,
                                'orderid'=>$common_order->id,
                                'orderdt_id'=>$data['order_details_id'],
                                'out_orderid'=>$new_order['msg'],
                                'account_flow'=>2,
                                'type'=>1,
                                'amount'=>$common_sum_quota,
                                'describe'=>'在租订单详情转租',
                                'isdel'=>1,
                                'created_at'=>date('Y-m-d H:i:s',time()),
                                'updated_at'=>date('Y-m-d H:i:s',time())
                            ]);
                            if($outrent_do_quota_to_account<1){
                                DB::rollback();return '转租订单额度值记录失败!';
                            }
                        }
                    } else { //剩余额度值不够返回退租
                        $outrent_do_quota_to_account = DB::table('account')->insertGetId([
                            'userid'=>$common_user->id,
                            'orderid'=>$common_order->id,
                            'orderdt_id'=>$data['order_details_id'],
                            'out_orderid'=>null,
                            'account_flow'=>1,
                            'type'=>1,
                            'amount'=>$outrent_do_pay_backable_useraccount,
                            'describe'=>'退租商品信用额度现金',
                            'isdel'=>1,
                            'created_at'=>date('Y-m-d H:i:s',time()),
                            'updated_at'=>date('Y-m-d H:i:s',time())
                        ]);
                        if(!$outrent_do_quota_to_account) {
                            DB::rollback();return '用户余额详情表记录数据失败!';
                        }
                        $outrent_do_quota_to_quotadt = DB::table('quota_detail')->insertGetId([
                            'quota_detail_quota_id'=>$common_quota->quota_id,
                            'quota_detail_order_id'=>$common_order->id,
                            'quota_detail_amount'=>($common_sum_quota-$outrent_do_pay_backable_useraccount),
                            'quota_detail_status'=>3,
                            'quota_detail_isdel'=>1,
                            'created_at'=>date('Y-m-d H:i:s',time()),
                            'updated_at'=>date('Y-m-d H:i:s',time())
                        ]);
                        if(!$outrent_do_quota_to_quotadt<1) {
                            DB::rollback();return '额度详情表记录数据失败!';
                        }
                        if($data['outrent_type']==1){
                            $outrent_do_quota_back_useraccount = DB::table('frontusers')->where('id',$data['userid'])->update(['account'=>$common_user->account+$outrent_do_pay_backable_useraccount,'updated_at'=>date('Y-m-d H:i:s',time())]);
                            if(!$outrent_do_quota_back_useraccount) {
                                DB::rollback();return '退租商品额度返回到用户余额失败!';
                            }
                            $outrent_do_quota_to_quota = DB::table('quota')->where('quota_id',$common_quota->quota_id)
                                ->update(['quota_current'=>$common_quota->quota_current+($common_sum_quota-$outrent_do_pay_backable_useraccount),'quota_used'=>$common_quota->quota_used-($common_sum_quota-$outrent_do_pay_backable_useraccount),'updated_at'=>date('Y-m-d H:i:s',time())]);
                            if(!$outrent_do_quota_to_quota) {
                                DB::rollback();return '退租额度表更新失败!';
                            }
                        }else{
                            $outrent_do_quota_to_account = DB::table('account')->insertGetId([
                                'userid'=>$common_user->id,
                                'orderid'=>$common_order->id,
                                'orderdt_id'=>$data['order_details_id'],
                                'out_orderid'=>$new_order['msg'],
                                'account_flow'=>2,
                                'type'=>1,
                                'amount'=>$outrent_do_pay_backable_useraccount,
                                'describe'=>'在租订单详情转租',
                                'isdel'=>1,
                                'created_at'=>date('Y-m-d H:i:s',time()),
                                'updated_at'=>date('Y-m-d H:i:s',time())
                            ]);
                            if($outrent_do_quota_to_account<1){
                                DB::rollback();return '转租订单额度值记录失败!';
                            }
                            $outrent_do_quota_to_quotadt_out = DB::table('quota_detail')->insertGetId([
                                'quota_detail_quota_id'=>$common_quota->quota_id,
                                'quota_detail_order_id'=>$new_order['msg'],//消费额度订单表主键；
                                'quota_detail_amount'=>($common_sum_quota-$outrent_do_pay_backable_useraccount),
                                'quota_detail_status'=>1,
                                'quota_detail_isdel'=>1,
                                'created_at'=>date('Y-m-d H:i:s',time()),
                                'updated_at'=>date('Y-m-d H:i:s',time())
                            ]);
                            if($outrent_do_quota_to_quotadt_out<1) {
                                DB::rollback();return '转租额度详情记录数据失败!';
                            }
                            $outrent_do_quota_to_update_new_order = DB::table('orders')->where('id',$new_order['msg'])->update(['orders_quota'=>($common_sum_quota-$outrent_do_pay_backable_useraccount)]);
                            if(!$outrent_do_quota_to_update_new_order){
                                DB::rollback();return '转租额度积分使用更新到转租订单表更新失败!';
                            }
                        }
                    }
                } else { //无可返回额度现金值
                    $outrent_do_quota_to_quotadt = DB::table('quota_detail')->insertGetId([
                        'quota_detail_quota_id'=>$common_quota->quota_id,
                        'quota_detail_order_id'=>$common_order->id,
                        'quota_detail_amount'=>$common_sum_quota,
                        'quota_detail_status'=>3,
                        'quota_detail_isdel'=>1,
                        'created_at'=>date('Y-m-d H:i:s',time()),
                        'updated_at'=>date('Y-m-d H:i:s',time())
                    ]);
                    if($outrent_do_quota_to_quotadt<1){
                        DB::rollback();return '额度详情表记录数据失败!';
                    }
                    if($data['outrent_type']==1){
                        $outrent_do_quota_to_quota = DB::table('quota')->where('quota_id',$common_quota->quota_id)->update(['quota_current'=>$common_quota->quota_current+$common_sum_quota, 'quota_used'=>$common_quota->quota_used-$common_sum_quota, 'updated_at'=>date('Y-m-d H:i:s',time())]);
                        if(!$outrent_do_quota_to_quota){
                            DB::rollback();return '额度表更新失败!';
                        }
                    } else{
                        $outrent_do_quota_to_quotadt_out = DB::table('quota_detail')->insertGetId([
                            'quota_detail_quota_id'=>$common_quota->quota_id,
                            'quota_detail_order_id'=>$new_order['msg'],//消费额度订单表主键；
                            'quota_detail_amount'=>$common_sum_quota,
                            'quota_detail_status'=>1,
                            'quota_detail_isdel'=>1,
                            'created_at'=>date('Y-m-d H:i:s',time()),
                            'updated_at'=>date('Y-m-d H:i:s',time())
                        ]);
                        if($outrent_do_quota_to_quotadt_out<1) {
                            DB::rollback();return '转租额度详情记录数据失败!';
                        }
                        $outrent_do_quota_to_update_new_order = DB::table('orders')->where('id',$new_order['msg'])->update(['orders_quota'=>$common_sum_quota]);
                        if(!$outrent_do_quota_to_update_new_order){
                            DB::rollback();return '转租额度积分使用更新到转租订单表更新失败!';
                        }
                    }
                }
            }
            DB::commit();
            return 'ok';
        }catch(Exception $e){
            DB::rollback();
            return '退租失败!';
        }
    }

    /***
     * 退租查询
     * @param查询参数
     */
    public static function outrentlist($param)
    {
        //
    }

    /***
     * 退租记录删除操作【隐藏】
     * @id 退租表id
     */
    public static function outrentdel($id)
    {
        //
    }

    /**在租售后处理*/
    public static function insert_repair($data)
    {
        //
    }

}
